/*
 * Copyright (c) 2010-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/graph/GraphicsContext.h"
#include "alkes/gfx/IGraphics3DDevice.h"
#include "alkes/math/Math.h"

namespace alkes {

GraphicsContext::GraphicsContext(IGraphics2DDevice* device)
: device_(device)
{
}

GraphicsContext::~GraphicsContext()
{
}

IGraphics2DDevice* GraphicsContext::getGraphics2DDevice()
{
    return device_;
}

IGraphics3DDevice* GraphicsContext::getGraphics3DDevice()
{
    return restrict_cast<IGraphics3DDevice*>(device_);
}

void GraphicsContext::addOffset(int16_t x, int16_t y)
{
    scissor_.addOffset(x, y);
}

void GraphicsContext::minusOffset(int16_t x, int16_t y)
{
    scissor_.minusOffset(x, y);
}

void GraphicsContext::pushGlobalScissor(const Rect& scissor)
{
    scissor_.pushGlobalScissor(scissor);
}

void GraphicsContext::pushLocalScissor(const Rect& scissor)
{
    scissor_.pushLocalScissor(scissor);
}

void GraphicsContext::pushScissorSize(int16_t w, int16_t h)
{
    scissor_.pushScissorSize(w, h);
}

void GraphicsContext::popScissorRect()
{
    scissor_.popScissorRect();
}

const Rect& GraphicsContext::getCurrentScissorRect()
{
    return scissor_.getCurrentRect();
}

const Rect& GraphicsContext::getPrevScissorRect()
{
    return scissor_.getPrevRect();
}

void GraphicsContext::setOpacity(int16_t opacity)
{
    opacity_ = static_cast<int16_t>(alkes_saturate<0, AL_OPACITY_MAX>(opacity));
}

int16_t GraphicsContext::getOpacity() const
{
    return opacity_;
}

const Color& GraphicsContext::modulateColor(Color& color)
{
    color.a = (Color::value_type)alkes_saturate<0, 255>((color.a * getOpacity()) / AL_OPACITY_MAX);
    return color;
}

}
